package com.lzc.tlmail.tlmailseckill.scheduled;

import com.lzc.tlmail.tlmailseckill.service.SeckillService;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 *
 * @author lzc
 * @create 2020-10-24 15:47
 */
@Slf4j
@Service
public class SeckillSkuScheduled {

    @Autowired
    private RedissonClient redissonClient;

    @Autowired
    private SeckillService seckillService;

    private final  String SESSIONSKU_LOCK="sessionsku_lock";

    /**
     * 每天晚上3点上架最近三天的秒杀商品
     */
    @Scheduled(cron="* * 3 * * ?")
    public void uploadSeckillSkuLatest3Days(){
        RLock lock = redissonClient.getLock(SESSIONSKU_LOCK);
        lock.lock(10, TimeUnit.SECONDS);
        try {
            log.info("上架秒杀商品信息");
            seckillService.uploadSeckillSkuLatest3Days();
        }finally {
            lock.unlock();
        }

    }
}
